package service.impl;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;

import dao.impl.TestDao;
import dao.interf.IListenDao;
import dao.interf.ITestDao;
import dao.pojo.Listen;
import dao.pojo.Passage;
import dao.pojo.Reading;
import dao.pojo.Rewrite;
import dao.pojo.Selection;
import dao.pojo.Test;
import dao.pojo.Translate;
import dao.pojo.Writing;

public class ParserTxt {
	private ITestDao testDao;


	public void setTestDao(ITestDao testDao) {
		this.testDao = testDao;
	}

	public void parse() {
		String fname = "d:/example.txt";
		InputStream fStream = null;
		try {
			fStream = new BufferedInputStream(new FileInputStream(fname));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.print("file can't open");
			return;
		}

		Test test = new Test();
		Scanner scan = new Scanner(fStream);
		String str = scan.nextLine();
		String temp = "";

		// ///////////////////////////
		Listen listen = new Listen();
		while (!str.matches(".*Part I\\..*") && scan.hasNext()) {
			str = scan.nextLine();
		}

		while (!str.matches(".*Passage:.*")) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replace("\\s+", " "));
		listen.setListenDirection(temp.replaceAll("\\s+", " "));// listen direction
		temp = "";

		while (!str.matches(".*Key:.*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replaceAll("\\s+", " "));
		listen.setListenPassge(temp.replaceAll("\\s+", " "));// listen passage
		temp = "";

		while (!str.matches(".*Part II\\..*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replaceAll("\\s+", " "));
		listen.setListenKeys(temp.replaceAll("\\s+", " "));// listen keys
		temp = "";

		test.setListen(listen);// listen

		// ////////////////////////////////////
		Selection selection = new Selection();
		while (!str.matches(".*Passage:.*") && scan.hasNext()) {
			temp = str;
			str = scan.nextLine();
		}

		System.out.println(temp.replaceAll("\\s+", " "));
		selection.setSelectionDirections(temp.replaceAll("\\s+", " "));// selection direction
		temp = "";

		while (!str.matches(".*[1-9]+\\..*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replaceAll("\\s+", " "));
		selection.setSelectionPassge(temp.replace("\\s+", " "));// selection passage
		temp = "";

		while (!str.matches(".*Key:.*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replace("\\s+", " "));
		selection.setSelections(temp.replace("\\s+", " "));// selection selections
		temp = "";

		while (!str.matches(".*Part III\\..*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replace("\\s+", " "));
		selection.setSelectionKeys(temp.replace("\\s+", " "));// selection keys
		temp = "";

		test.setSelection(selection);// selection

		// ////////////////////////////////////
		Translate translate = new Translate();
		while (!str.matches(".*[1-9]+\\..*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replace("\\s+", " "));
		translate.setTranslateDirection(temp.replace("\\s+", " "));// translate direction
		temp = "";

		while (!str.matches(".*Key:.*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replaceAll("\\s+", " "));
		translate.setTranslateTitle(temp.replaceAll("\\s+", " "));// translate title
		temp = "";

		while (!str.matches(".*Part IV\\..*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replaceAll("\\s+", " "));
		translate.setTranslateKey(temp.replaceAll("\\s+", " "));// translate keys
		temp = "";

		test.setTranslate(translate);// translate

		// //////////////////////////////
		Rewrite rewrite = new Rewrite();
		while (!str.matches(".*[1-9]+\\..*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replace("\\s+", " "));
		rewrite.setRewriteDirection(temp.replace("\\s+", " "));// rewrite direction
		temp = "";

		while (!str.matches(".*Key:.*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replaceAll("\\s+", " "));
		rewrite.setRewriteTitle(temp.replaceAll("\\s+", " "));// rewrite title
		temp = "";

		while (!str.matches(".*Part V\\..*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replaceAll("\\s+", " "));
		rewrite.setRewriteKeys(temp.replaceAll("\\s+", " "));// rewrite keys
		temp = "";

		test.setRewrite(rewrite);// rewrite

		// //////////////////////////////
		Reading read = new Reading();
		Passage passageI = new Passage();
		Passage passageII = new Passage();
		while (!str.matches(".*Passage I.*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replace("\\s+", " "));
		read.setReadDirection(temp.replace("\\s+", " "));// read direction
		temp = "";

		while (!str.matches(".[1-9]+\\..*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replaceAll("\\s+", " "));
		passageI.setPassageContext(temp.replaceAll("\\s+", " "));
		read.setPassageByPassageIid(passageI);// read passage I
		temp = "";

		while (!str.matches(".*Key:.*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}

		System.out.println(temp.replaceAll("\\s+", " "));
		read.setReadI(temp.replaceAll("\\s+", " "));// read I
		temp = "";
		
		while (!str.matches(".*Passage II.*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}
		
		System.out.println(temp.replaceAll("\\s+", " "));
		read.setReadIkeys(temp.replaceAll("\\s+", " "));// read I keys
		temp = "";
		
		while (!str.matches(".*[1-9]+\\..*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}
		
		System.out.println(temp.replaceAll("\\s+", " "));
		passageII.setPassageContext(temp.replaceAll("\\s+", " "));
		read.setPassageByPassageIiid(passageII);// read passage II
		temp = "";
		
		while (!str.matches(".*Part VI\\..*") && scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}
		
		System.out.println(temp.replaceAll("\\s+", " "));
		read.setReadIikeys(temp.replaceAll("\\s+", " "));// read II keys
		temp = "";
		
		test.setReading(read);// read
		
		//////////////////////////////
		Writing write = new Writing();
		while (scan.hasNext()) {
			temp += str;
			str = scan.nextLine();
		}
		
		System.out.println(temp.replaceAll("\\s+", " "));
		write.setWriteContent(temp.replaceAll("\\s+", " "));// write context
		temp = "";
		
		test.setWriting(write);// writing
		
		testDao.add(test);
	}

}
